#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "lib.h"
using namespace std;

const int _buf_size = 10000000;

int main(int argc, char *argv[])
{
	srand(123456789);
	if (argc != 4) {
		printf("%s: error number of parameters!\n", argv[0]);
		exit(1);
	}
	int dim = s_to_i(argv[3]);

	FILE *f1 = fopen(argv[1], "r");
	FILE *f2 = fopen(argv[2], "w");
	if (f1 == NULL || f2 == NULL) {
		printf("%s: cannot open file(s)!\n", argv[0]);
		exit(1);
	}
	setvbuf(f1, NULL, _IOFBF, _buf_size);
	double *a = new double[dim];
	int cnt = 0;
	while (1) {
		int lab;
		if (fscanf(f1, "%d", &lab) < 1) break;
		for (int i = 0; i < dim; ++i)
			if(fscanf(f1, "%lf", &a[i]) < 1) {
				printf("error!\n");
				return 2;
			}
		fprintf(f2, "%d", lab);
		for (int i = 0; i < dim; ++i)
			if (a[i] != 0)
				fprintf(f2, " %d:%.8lf", i+1, a[i]);
		fprintf(f2, "\n");
		++cnt;
		if (cnt % 500 == 0) printf("transformed %d.\n", cnt);
	}
	fclose(f1);
	fclose(f2);

	printf("%s: done.\n", argv[0]);

	return 0;
}

